package com.qingke.learn;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

public class LearnFileIO {
	public static void main(String[] args){
		
		
//		String folderPath="C:\\Users\\qingke05\\Desktop\\ceshi";
		String fileName="C:\\Users\\qingke05\\Desktop\\ceshi\\Core Java.xmind";
		String fileName2="C:\\Users\\qingke05\\Desktop\\ceshi\\newCore Java.xmind";
//		upSet(fileName);
		copy(fileName,fileName2);
//		copy(fileName,fileName2);
//		createFile(fileName);
//		createFile(fileName2);
//		writeToFile(fileName);
//		readFromFile(fileName2);
//		deleteFile(fileName);
//		listFiles(folderPath);
//		createFolder(folderPath);
//		listFiles(folderPath);
//		deleteFolder(folderPath);
//		listFiles(folderPath);
		
		
	}
	
	public static void upSet(String fileName){
		File file = new File(fileName);
		List<String> list = new ArrayList<String>();
		Random r = new Random();
		if(!file.exists()){
			System.out.println("文件不存在，读取失败");
		}
		else if(file.isFile()){
			BufferedReader br = null;
			BufferedWriter bw = null;
			try {
				FileReader reader = new FileReader(file);
				br = new BufferedReader(reader);
				
				String line = br.readLine();
				while(line!=null){
//					System.out.println(line);
					list.add(line);
					line=br.readLine();
				}
				System.out.println(list.size());
				System.out.println("读取成功");
				
				Collections.sort(list,new Comparator<String>(){
					public int compare(String o1, String o2) {
						return r.nextInt(3)-1;
					}
				});
				
				FileWriter writer = new FileWriter(file);
				bw = new BufferedWriter(writer);
				for(String str:list){
					str=upSetLine(str);
					bw.write(str+"\r\n");
//					System.out.println(str);
				}
				bw.flush();
				System.out.println("打乱成功");
				
				
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				if(br!=null){
					try {
						br.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		else{
			System.out.println("这不是一个文件，无法读取！");
		}
		
		
	}
	
	public static String upSetLine(String line){
		char[] arr=line.toCharArray();
		Random r = new Random();
		int length=arr.length;
		int j,k;
		char ch;
		for(int i=0;i<length;i++){
			j=r.nextInt(length);
			k=r.nextInt(length);
			if(j!=k){
				ch=arr[j];
				arr[j]=arr[k];
				arr[k]=ch;
			}
		}
		StringBuilder str = new StringBuilder();
		for(char c:arr){
			str.append(c);
		}
		return str.toString();
	}

	public static void copy(String fileName,String newFileName){
		if(fileName==newFileName){
			System.out.println("文件名不能重复");
			return ;
		}
		File file = new File(fileName);
		File newFile = new File(newFileName);
		if(!file.exists()){
			System.out.println("文件不存在，拷贝失败！");
		}
		else if(file.isFile()){
			copyFile(fileName,newFileName);
		}
		else if(file.isDirectory()){
			newFile.mkdirs();
			for(String name:file.list()){
				System.out.println(name);
				copy(fileName+"\\"+name,newFileName+"\\"+name);
			}
		}
	}
	
	public static void copyFile(String fileName,String newName){
		File file = new File(fileName);
		File newFile = new File(newName);
		
		if(!file.exists()){
			System.out.println("文件不存在，拷贝失败！");
		}
		else if(file.isDirectory()){
			System.out.println("这不是一个文件，拷贝失败！");
		}
		else{
			//只能拷贝文本，拷贝其他并不能成功，大小会变，内容也乱了
//			BufferedReader br=null;
//			BufferedWriter bw=null;
			//可以拷贝任意格式的文件
			FileInputStream input=null;
			FileOutputStream output=null;
			byte[] b=new byte[128];
			int length;
			
			try {
				if(!newFile.exists()){
					newFile.createNewFile();
				}
//				FileReader reader = new FileReader(file);
//				FileWriter writer = new FileWriter(newFile);
//				br = new BufferedReader(reader);
//				bw = new BufferedWriter(writer);
//				String line=br.readLine();
//				while(line!=null){
//					bw.write(line);
//					bw.newLine();
//					line=br.readLine();
//				}
//				bw.flush();
				
				input = new FileInputStream(file);
				output = new FileOutputStream(newFile);
				while(true){
					length=input.read(b);
					if(length==-1){
						break;
					}
					output.write(b,0,length);
					output.flush();
				}
				System.out.println("拷贝成功！");
				
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
//				if(br!=null){
//					try {
//						br.close();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//				}
//				if(bw!=null){
//					try {
//						bw.close();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//				}
				if(input!=null){
					try {
						input.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(output!=null){
					try {
						output.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
	}
	
	public static void readFromFile(String fileName){
		File file = new File(fileName);
		if(!file.exists()){
			System.out.println("文件不存在，读取失败");
		}
		else if(file.isFile()){
			BufferedReader br = null;
			try {
				FileReader reader = new FileReader(file);
				br = new BufferedReader(reader);
				
				System.out.println("=================");
				String line = br.readLine();
				while(line!=null){
					System.out.println(line);
					line=br.readLine();
				}
				System.out.println("=================");
				System.out.println("读取成功");
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				if(br!=null){
					try {
						br.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		else{
			System.out.println("这不是一个文件，无法读取！");
		}
	}
	
	public static void writeToFile(String fileName){
		File file = new File(fileName);
		if(!file.exists()){
			System.out.println("文件不存在，写入失败");
		}
		else if(file.isFile()){
			BufferedWriter bw = null;
			try {
				FileWriter writer = new FileWriter(file);
				bw = new BufferedWriter(writer);
				
				bw.write("hello\r\n");
				bw.write("hello\r\n");
				bw.write("hello\r\n");
				
				bw.flush();
				System.out.println("写入成功");
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				if(bw!=null){
					try {
						bw.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		else{
			System.out.println("这不是一个文件，无法写入！");
		}
	}
	public static boolean createFile(String fileName){
		boolean isSuccess=false;
		File file = new File(fileName);
		try {
			isSuccess=file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return isSuccess;
	}
	public static boolean deleteFile(String fileName){
		boolean isSuccess=false;
		File file = new File(fileName);
		isSuccess=file.delete();
		return isSuccess;
	}
	public static boolean createFolder(String folderPath){
		boolean isSuccess=false;
		File folder = new File(folderPath);
		isSuccess=folder.mkdirs();
		return isSuccess;
	}
	public static boolean deleteFolder(String folderPath){
		boolean isSuccess=false;
		File folder = new File(folderPath);
		isSuccess=deleteAll(folder);
		System.out.println("删除文件夹："+isSuccess);
		return isSuccess;
	}
	public static void listFiles(String folderPath){
		File file = new File(folderPath);

		System.out.println("=================");
		if(file.exists()){
			for(String fileName:file.list()){
				System.out.println(fileName);
			}
		}
	}
	public static boolean deleteAll(File folder){
		boolean isSuccess=false;
		for(File fileName:folder.listFiles()){
			if(fileName.isDirectory())
				isSuccess=deleteAll(fileName);
			isSuccess=fileName.delete();
		}
		return isSuccess;
	}
}
